{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python面向对象\n",
    "\n",
    "### 1、 什么是对象\n",
    "\n",
    "属性+方法=类\n",
    "\n",
    "同一个类可以生成无数个对象\n",
    "\n",
    "一只股票的属性： 市值，成交量，价格...\n",
    "\n",
    "方法： 获取股票的市值、成交量，计算成交额"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、 通常命名方式\n",
    "- 类名用大写\n",
    "- 属性为名词\n",
    "- 方法为动词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "calAmount() takes 0 positional arguments but 1 was given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-89ad93b50f09>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;31m# 实例化类对象\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mcheckStockA\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstockA\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mcheckStockA\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcalAmount\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: calAmount() takes 0 positional arguments but 1 was given"
     ]
    }
   ],
   "source": [
    "class StockA:\n",
    "    price=20\n",
    "    volume=10000\n",
    "    def calAmount():\n",
    "        return price*volume\n",
    "\n",
    "# 实例化类对象\n",
    "checkStockA = StockA()\n",
    "checkStockA.calAmount()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、 self\n",
    "类方法必须包含参数 self,且为第一个参数,隐藏并默认的要求。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class StockA:\n",
    "    marketvalue=50000\n",
    "    price=20\n",
    "    volume=10000\n",
    "    def calAmount(self):\n",
    "        return self.price*self.volume\n",
    "\n",
    "# 实例化类对象\n",
    "checkStockA = StockA()\n",
    "checkStockA.calAmount()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、 一个类生成多个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'BABA_Amount': 5400000}\n",
      "{'Tencent_Amount': 7000000}\n",
      "{'BIDU_Amount': 2300000}\n"
     ]
    }
   ],
   "source": [
    "class stock:\n",
    "    def getToday(self,stockName, price, volume):\n",
    "        self.stockName=stockName\n",
    "        self.price = price\n",
    "        self.volume = volume\n",
    "    def calAmount(self):\n",
    "        return {self.stockName+'_Amount': self.price*self.volume}\n",
    "\n",
    "# 实例化类创建类对象\n",
    "stockBABA = stock()\n",
    "stockBABA.getToday('BABA', 180, 30000)\n",
    "print(stockBABA.calAmount())\n",
    "\n",
    "stockTencent = stock()\n",
    "stockTencent.getToday('Tencent', 350, 20000)\n",
    "print(stockTencent.calAmount())\n",
    "\n",
    "stockBIDU = stock()\n",
    "stockBIDU.getToday('BIDU', 230, 10000)\n",
    "print(stockBIDU.calAmount())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5、init\n",
    "\n",
    "实例化时自动运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'BABA_Amount': 5400000}\n",
      "{'Tencent_Amount': 7000000}\n",
      "{'BIDU_Amount': 2300000}\n"
     ]
    }
   ],
   "source": [
    "class stock:\n",
    "    def __init__(self,stockName, price, volume):\n",
    "        self.stockName=stockName\n",
    "        self.price = price\n",
    "        self.volume = volume\n",
    "    def calAmount(self):\n",
    "        return {self.stockName+'_Amount': self.price*self.volume}\n",
    "\n",
    "stockBABA = stock('BABA', 180, 30000)\n",
    "print(stockBABA.calAmount())\n",
    "\n",
    "stockTencent = stock('Tencent', 350, 20000)\n",
    "print(stockTencent.calAmount())\n",
    "\n",
    "stockBIDU = stock('BIDU', 230, 10000)\n",
    "print(stockBIDU.calAmount())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6、 公有/私有变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'BABA'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 公有变量名\n",
    "\n",
    "class stockBABA:\n",
    "    name = 'BABA'\n",
    "stockName = stockBABA()\n",
    "stockName.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'stockBABA' object has no attribute '__name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-aa8ac6c7289a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0m__name\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'BABA'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mstockName\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstockBABA\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mstockName\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__name\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'stockBABA' object has no attribute '__name'"
     ]
    }
   ],
   "source": [
    "# 私有变量名\n",
    "class stockBABA:\n",
    "    __name = 'BABA'\n",
    "stockName = stockBABA()\n",
    "stockName.__name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'BABA'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 伪私有变量名： _类名__变量名\n",
    "\n",
    "class stockBABA:\n",
    "    __name = 'BABA'\n",
    "stockName = stockBABA()\n",
    "stockName._stockBABA__name"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
